var userAgent 	= navigator.userAgent.toLowerCase();
var is_webtv 	= userAgent.indexOf('webtv') != -1;
var is_kon 		= userAgent.indexOf('konqueror') != -1;
var is_mac 		= userAgent.indexOf('mac') != -1;
var is_saf 		= userAgent.indexOf('applewebkit') != -1 || navigator.vendor == 'Apple Computer, Inc.';
var is_opera 	= userAgent.indexOf('opera') != -1 && opera.version();
var is_moz 		= (navigator.product == 'Gecko' && !is_saf) && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ns 		= userAgent.indexOf('compatible') == -1 && userAgent.indexOf('mozilla') != -1 && !is_opera && !is_webtv && !is_saf;
var is_ie 		= (userAgent.indexOf('msie') != -1 && !is_opera && !is_saf && !is_webtv) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var isIE=-1!=navigator.userAgent.indexOf('MSIE');
// 鼠标hover高亮
$(document).ready(function(event) {
	if($(".rollbg")){
		$(".rollbg").hover(
			function(){ $(this).addClass("bg");},
			function(){ $(this).removeClass("bg"); }
		)
	}
    /*设置body宽度*/
    var winW = $(document).width();
    $("body").css({"width":winW});
    $(window).on('resize',function(){
        var winW = $(document).width();
        $("body").css({"width":winW});
    });
});
// jquery扩展集合
$.extend($, {
    /* 
    * ajax调用封装，返回 response
    * data一般为form.serialize() 
    * callback 回调函数
    */
    noyaAjax: function(url, data, callback) {
    	// noyaLoading(true); // 用来做载入提示
        $.ajax({
            url: url + "&r=" + (new Date().getTime()),
            data: data,
            type: "POST",
			//timeout: 15000,
            success: function(response) {
                // noyaLoading(false);
                if (typeof callback != 'undefined')
                    callback.call(this, response);
            }
        });
    }
});
// TAB打开页面
function open_tab(tabid,url,name) {
	parent.addTab( tabid, url, name);
}
// TAB关闭页面
function close_tab() {
	parent.closeTab();
}
// 是否在数组
function in_array(needle, haystack) {
	if(typeof needle == 'string') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}
// 列表上的全选
function checkall(refer_id, checkbox_name) {
	$("#"+refer_id).click(function() {
        $('input[name="'+checkbox_name+'"]').attr("checked",this.checked); 
    });
    var $sub_box = $("input[name='"+checkbox_name+"']");
    $sub_box.click(function(){
        $("#"+refer_id).attr("checked",$sub_box.length == $("input[name='"+checkbox_name+"']:checked").length ? true : false);
    });
}
// 跳转
function redirect(url) {
	window.location.replace(url);
}
// 获得cookie
function getcookie(name) {
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}
// 设置cookie
function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
	var expires = new Date();
	expires.setTime(expires.getTime() + seconds);
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}
/////////////////////////////////////////////////////////////////表单验证相关///////////////////////////////////////////////////////////////////////
// 清空前后空格
function trim(str) {
	return (str + '').replace(/\s+$/g, '').replace(/^\s+/g, '');
}
// 得到字符数
function strlen(str) {
	return str.replace(/[^\x00-\xff]/g, "**").length;
}
// 是否是日期
function isdate(str) {
	var ret = str.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/);
	if(ret == null) return false;
	ret[2] --;
	var d = new Date(ret[1],ret[2],ret[3]);
	return d.getFullYear() == ret[1] && d.getMonth() == ret[2] && d.getDate() == ret[3];
}
// 是否是数字
function isnumber(str) {
	var reg = /^(-?\d+)(\.\d+)?$/;
	return reg.test(str);
}
// 是否是单词
function isnumberletter(str) {
	var reg = /^\w+$/;
	return reg.test(str);
}
// 是否是字母
function isletter(str) {
	var reg = /^[a-zA-Z]+$/;
	return reg.test(str);
}
// 是否是整数
function isint(str) {
	var reg = /^-?\d+$/;
	return reg.test(str);
}
//  是否是email
function isemail(str) {
	var reg = /([\w|_|\.|\+]+)@([-|\w]+)\.([A-Za-z]{2,4})/;
	return reg.test(str);
}
// 科学输出num
function num_format (num) {
    return (num.toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
}
// 生成下级OPTION,级联菜单使用
function _get_option(fVal,sVal,select_struct,cid,fid,cname) {
	// 得到该fval的子集
    var option = '<option value="0">请选择</option>';
	for (i=0; i<select_struct.length; i++) {
		eval("var tcid = select_struct["+i+"]."+cid+";");
		eval("var tfid = select_struct["+i+"]."+fid+";");
		eval("var tcname = select_struct["+i+"]."+cname+";");
		if(fVal==tfid) {			
        	 if(sVal == tcid ) {
        		var selected = 'selected="selected"';
        	} else {
        		var selected = '';
        	}
            option += "<option value='" + tcid + "' "+selected+">" + tcname + "</option>";
		}
	}
	return option;
}
// 生成下级li_div,级联菜单使用
function _get_li_div(fVal,sVal,select_struct,cid,fid,cname) {
	// 得到该fval的子集
    //var option = "<div class='linkage_li selected' data-value='0'>请选择</div>";
    var option = "";
	for (i=0; i<select_struct.length; i++) {
		eval("var tcid = select_struct["+i+"]."+cid+";");
		eval("var tfid = select_struct["+i+"]."+fid+";");
		eval("var tcname = select_struct["+i+"]."+cname+";");
		if(fVal==tfid) {			
        	 if(sVal == tcid ) {
        		var selected = 'selected';
        	} else {
        		var selected = '';
        	}
            option += "<div class='linkage_li " + selected + "' data-value='"+ tcid +"'>" + tcname + "</div>";
		}
	}
	return option;
}
// 分页输入页面跳转
function goto_page() {
	var to_page = $("#to_page").val();
	to_page = parseInt(to_page);
	to_page = isNaN(to_page) ? 0 : to_page;
	var current_url = location.href;
	var t = current_url.indexOf('page=');
	var goto_url = '';
	if(t>0) {
		goto_url = current_url.replace(/page=([0-9]+)/i,"page="+to_page); 
	} else {
    	var t1 = current_url.indexOf('?');
    	if(t1>0) {
    		goto_url = current_url+ '&page='+to_page;
    	} else {
    		goto_url = current_url+ '?page='+to_page;
    	}
	}
	location.href = goto_url;
}

